package com.douqu.game.mergeManager.mergeUtils;

import com.douqu.game.MainFrame;
import com.douqu.game.MenuManager;
import com.douqu.game.core.entity.common.TwoTuple;
import com.douqu.game.core.util.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author: miracle.
 * Description:
 * Date: 2018/6/8 0008 上午 10:30
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class MegerJtable {
    EvenOddRowCellRenderer even;

    private List<DatabaseConfig> databaseConfigs;

    public MegerJtable(int number,int y,Container container,MainFrame frame){
        even = new EvenOddRowCellRenderer(number,y,container,frame);
    }

    public TwoTuple<Boolean,String> initData(MenuManager manager){
        TwoTuple<Boolean,String> twoTuple = new TwoTuple<>();
        databaseConfigs = new ArrayList<>();
        //获取目标数据库
        String ip;
        String port;
        String dbTitle;
        String user;
        String password;
        JTable table = even.getTable();
        String servereType;
        if (even.getNumber() == 1){
            servereType = "目标服务器";
        }else{
            servereType = "合并服务器";
        }
        for (int i = 0;i < even.getNumber(); i ++){
            if (table.getValueAt(i,0) == null){
//                twoTuple.setFirst(false);
//                twoTuple.setSecond(servereType + ",第" +(i + 1) + "行ip不能为空");
//                return twoTuple;
                continue;
            }else{
                ip = table.getValueAt(i,0).toString();
                ip.trim();
                if (StringUtils.isNullOrEmpty(ip)){
//                    twoTuple.setFirst(false);
//                    twoTuple.setSecond(servereType + ",第" +(i + 1) + "行ip不能为空");
//                    return twoTuple;
                    continue;
                }
                //正则验证ip
                if (!isIp(ip)){
                    twoTuple.setFirst(false);
                    twoTuple.setSecond(servereType + ",第" +(i + 1) + "行ip格式错误");
                    manager.println(servereType + ",第" +(i + 1) + "行ip格式错误");
                    return twoTuple;
                }
            }

            if (table.getValueAt(i,1) == null){
                twoTuple.setFirst(false);
                twoTuple.setSecond(servereType + ",第" +(i + 1) + "行端口不能为空");
                manager.println(servereType + ",第" +(i + 1) + "行端口不能为空");
                return twoTuple;
            }else{
                port = table.getValueAt(i,1).toString();
                port.trim();
                if (StringUtils.isNullOrEmpty(port)){
                    twoTuple.setFirst(false);
                    twoTuple.setSecond(servereType + ",第" +(i + 1) + "行端口不能为空");
                    manager.println(servereType + ",第" +(i + 1) + "行端口不能为空");
                    return twoTuple;
                }
                //验证端口
                if (!isPort(port)){
                    twoTuple.setFirst(false);
                    twoTuple.setSecond(servereType + ",第" +(i + 1) + "行端口格式错误");
                    manager.println(servereType + ",第" +(i + 1) + "行端口格式错误");
                    return twoTuple;
                }
            }

            if (table.getValueAt(i,2) == null){
                twoTuple.setFirst(false);
                twoTuple.setSecond(servereType + ",第" +(i + 1) + "行数据库不能为空");
                manager.println(servereType + ",第" +(i + 1) + "行数据库不能为空");
                return twoTuple;
            }else{
                dbTitle = table.getValueAt(i,2).toString();
                dbTitle.trim();
                if (StringUtils.isNullOrEmpty(dbTitle)){
                    twoTuple.setFirst(false);
                    twoTuple.setSecond(servereType + ",第" +(i + 1) + "行数据库不能为空");
                    manager.println(servereType + ",第" +(i + 1) + "行数据库不能为空");
                    return twoTuple;
                }
            }

            if (table.getValueAt(i,3) == null){
                twoTuple.setFirst(false);
                twoTuple.setSecond(servereType + ",第" +(i + 1) + "行用户名不能为空");
                manager.println(servereType + ",第" +(i + 1) + "行用户名不能为空");
                return twoTuple;
            }else{
                user = table.getValueAt(i,3).toString();
                user.trim();
                if (StringUtils.isNullOrEmpty(user)){
                    twoTuple.setFirst(false);
                    twoTuple.setSecond(servereType + ",第" +(i + 1) + "行用户名不能为空");
                    manager.println(servereType + ",第" +(i + 1) + "行用户名不能为空");
                    return twoTuple;
                }
            }

            if (table.getValueAt(i,4) == null){
                twoTuple.setFirst(false);
                twoTuple.setSecond(servereType + ",第" +(i + 1) + "行密码不能为空");
                manager.println(servereType + ",第" +(i + 1) + "行密码不能为空");
                return twoTuple;
            }else{
                password = table.getValueAt(i,4).toString();
                password.trim();
                if (StringUtils.isNullOrEmpty(password)){
                    twoTuple.setFirst(false);
                    twoTuple.setSecond(servereType + ",第" +(i + 1) + "行密码不能为空");
                    manager.println(servereType + ",第" +(i + 1) + "行密码不能为空");
                    return twoTuple;
                }
            }
            DatabaseConfig databaseConfig = new DatabaseConfig(ip,port,dbTitle,user,password);
            databaseConfigs.add(databaseConfig);
            manager.println(databaseConfig.getUrl());
        }

        if (even.getNumber() == 1){
            if (databaseConfigs.isEmpty()){
                twoTuple.setFirst(false);
                twoTuple.setSecond("请填入正确的目标服务器");
                manager.println("请填入正确的目标服务器");
                return twoTuple;
            }
        }else{
            if (databaseConfigs.size() < 2){
                twoTuple.setFirst(false);
                twoTuple.setSecond("合并服务器不能少于两个");
                manager.println("合并服务器不能少于两个");
                return twoTuple;
            }
        }

        twoTuple.setFirst(true);
        twoTuple.setSecond("");
        return twoTuple;
    }

    public boolean isIp(String ip){
        String regex = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
        return Pattern.matches(regex,ip);
    }

    public boolean isPort(String port){
        String regex = "^([1-9]|[1-9]\\d{3}|[1-6][0-5][0-5][0-3][0-5])$";
        return Pattern.matches(regex,port);
    }

    public List<DatabaseConfig> getDatabaseConfigs() {
        return databaseConfigs;
    }

    public void setDatabaseConfigs(List<DatabaseConfig> databaseConfigs) {
        this.databaseConfigs = databaseConfigs;
    }
}
